//add question
function submit() {

    var category = document.getElementById("old_or_new_category_select").value == 'old' ? document.getElementById('old_category').value : document.getElementById('new_category').value;
    //public or not
    var public = document.getElementById("creator").value;
    var level=document.getElementById("difficulty_level").value;
    var question = document.getElementById("qst").value;
    var answer = document.getElementById("ans").value;

   
    let data = {category:category,level:level,question:question,answer:answer,public:public};
    function ok(){
        console.log(this.responseText);
        if(this.responseText == "success"){
            alert("add successfully!")
            location.replace('../html/questionmanage.html');
        }else{
            alert(this.responseText);
        }
    }
    function ko(){
        alert("Something wrong with addquestion!")
    }
    
    ajaxRequest("../php/addquestion.php",'post',data,ok,ko);
   

    
    
}
var menu = [];


window.onload = function () {
    function isLogin(){
        if(localStorage.hasOwnProperty("UserName")){
            document.getElementById("login").style.display = 'none';
            document.getElementById("login-out").style.display = 'block';
            document.getElementById("profile").style.display = 'block';
            document.querySelector("#profile a").innerHTML = 'User: ' + localStorage.getItem('UserName');
        }
        else{
            document.getElementById("login-out").style.display = 'none';
            document.getElementById("login").style.display = 'block';
            document.getElementById("profile").style.display = 'none';
        }
    }
    isLogin();
    function ok() {
        console.log(this.responseText);
        let json_string = this.responseText;
        let res_object = JSON.parse(json_string);
        for (var i = 0; i < res_object.length; i++) {
            menu.push(res_object[i].category);
        }
        initCategoryMenu();
    }

    function ko() {
        console.log(this.responseText);
        alert("Something wrong with get category");
    }

    function getCategoy() {
        ajaxRequest("../php/getCategory.php", 'get', null, ok, ko);
    }

    function initCategoryMenu() {
        let old_cat_menu = document.getElementById("old_category");

        for (var i = 0; i < menu.length; i++) {
            var option = document.createElement("option");
            option.setAttribute('value', menu[i]);
            option.innerHTML = menu[i];

            old_cat_menu.appendChild(option);
        }
    }

    getCategoy();
    let old_cat = document.getElementById('old_category');
    let new_cat = document.getElementById('new_category');
    let cat_select = document.getElementById('old_or_new_category_select');

    old_cat.style.display = 'none';
    new_cat.style.display = 'none';
    cat_select.addEventListener('change', function () {
        if (cat_select.value == 'old') {
            old_cat.style.display = 'block';
            new_cat.style.display = 'none';
        } else if (cat_select.value == 'new') {
            old_cat.style.display = 'none';
            new_cat.style.display = 'block';
        } else {
            old_cat.style.display = 'none';
            new_cat.style.display = 'none';
        }
    })
}
